﻿using System;
using System.Data;
using CoreLab.Oracle; //using Devart.Data.Oracle; //using Oracle.DataAccess.Client;
using System.Collections.Generic;

using LMD.Constant;
using LMD.Lib.DataClass;
using LMD.Lib.Oracle;

namespace DBQuery
{
    /// <summary>
    /// Summary description for KonfigurasiDatekTable
    /// </summary>
    public static class KonfigurasiDatekTable
    {
        public static void SaveCDData(CDData cdData)
        {
            DBLib dbl = new DBLib();
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "out_new_id";
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Output;
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_NOTE";
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Input;
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            if (String.IsNullOrEmpty(cdData.sxNote))
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = cdData.sxNote;
            }
            
            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_INFO";
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Input;
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            if (String.IsNullOrEmpty(cdData.sxInfo))
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = cdData.sxInfo;
            }
            
            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_CREATE_BY";
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Input;
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = ICPUser.ID;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_KONFIG_FILE_ID";
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Input;
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            if (cdData.iConfigFileID < 0)
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = cdData.iConfigFileID;
            }
            
            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_DATEK_FILE_ID";
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Input;
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            if (cdData.iDatekFileID < 0)
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = cdData.iDatekFileID;
            }

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_EVALUASI_FILE_ID";
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Input;
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            if (cdData.iEvaluasiFileID < 0)
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = cdData.iEvaluasiFileID;
            }
           
            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "in_CUSTOMER_ID";
            lsParams[lsParams.Count - 1].Direction = ParameterDirection.Input;
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = cdData.iCustomerID;

            String sxQuery = "pr_insert_T_KONFIGURASI_DATEK";

            int i = dbl.ExecuteProcedure(sxQuery, lsParams.ToArray());
            
        }

        public static void EditCDData(CDData cdData)
        {
            DBLib dbl = new DBLib();
            List<OracleParameter> lsParams = new List<OracleParameter>();

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmNote";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            //lsParams[lsParams.Count - 1].Value = cdData.sxNote;
            if (String.IsNullOrEmpty(cdData.sxNote))
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = cdData.sxNote;
            }

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmInfo";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            //lsParams[lsParams.Count - 1].Value = cdData.sxInfo;
            if (String.IsNullOrEmpty(cdData.sxInfo))
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = cdData.sxInfo;
            }

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmUpdateBy";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.VarChar;
            lsParams[lsParams.Count - 1].Value = ICPUser.ID;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmKonfigFileId";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            //lsParams[lsParams.Count - 1].Value = cdData.iConfigFileID;
            if (cdData.iConfigFileID < 0)
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = cdData.iConfigFileID;
            }

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmDatekFileId";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            //lsParams[lsParams.Count - 1].Value = cdData.iDatekFileID;
            if (cdData.iDatekFileID < 0)
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = cdData.iDatekFileID;
            }

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmEvaluasiFileId";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            //lsParams[lsParams.Count - 1].Value = cdData.iEvaluasiFileID;
            if (cdData.iEvaluasiFileID < 0)
            {
                lsParams[lsParams.Count - 1].Value = DBNull.Value;
            }
            else
            {
                lsParams[lsParams.Count - 1].Value = cdData.iEvaluasiFileID;
            }

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmCustomerId";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = cdData.iCustomerID;

            lsParams.Add(new OracleParameter());
            lsParams[lsParams.Count - 1].ParameterName = "prmCDID";
            lsParams[lsParams.Count - 1].OracleDbType = OracleDbType.Number;
            lsParams[lsParams.Count - 1].Value = cdData.iCDID;

            String sxQuery =
                @"
                update
                    T_KONFIGURASI_DATEK
                set
                    NOTE = :prmNote,
                    INFO = :prmInfo,
                    UPDATE_BY = :prmUpdateBy,
                    UPDATE_DATE = sysdate,
                    KONFIG_FILE_ID = :prmKonfigFileId,
                    DATEK_FILE_ID = :prmDatekFileId,
                    EVALUASI_FILE_ID = :prmEvaluasiFileId
                where
                    CUSTOMER_ID = :prmCustomerId
                    and T_KONFIGURASI_DATEK_ID = :prmCDID
                ";

            int i = dbl.ExecuteNonQuery(sxQuery, lsParams.ToArray());
            
        }
    }
}